#include "client.h"

int recvfile(int sockfd,const char *path){
    train_t train;
    int fd = open(path,O_RDWR|O_CREAT,0666);
    off_t filesize;
    recvn(sockfd,&train.length,sizeof(train.length));
    recvn(sockfd,train.buf,train.length);
    memcpy(&filesize,train.buf,sizeof(filesize));
    ftruncate(fd,filesize);
    char *p = (char *)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    recvn(sockfd,p,filesize);
    munmap(p,filesize);
    close(fd);
    return 0;
}

